package yyee.llee.mmee.viewmode

import androidx.lifecycle.MutableLiveData
import yyee.llee.mmee.base_api_net.base_api_bean.bean.AirCityInfoBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.AirCityQuality
import yyee.llee.mmee.base_api_net.base_api_bean.bean.BaseResult
import yyee.llee.mmee.base_api_net.base_api_bean.bean.CityBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class AirQualityChildViewModel : BaseViewModel() {
    val airNow = MutableLiveData<ArrayList<AirCityInfoBean>>()
    val airDay = MutableLiveData<ArrayList<AirCityQuality>>()
    private val airRepository = AirRepository()
    private lateinit var cityBean: CityBean

    fun setCityBean(bean: CityBean) {
        cityBean = bean;
    }

    fun getCityBean(): CityBean {
        return cityBean
    }

    fun loadData(cityName: String) {

        launchUI {
            val result = withContext(Dispatchers.IO) {
                airRepository.getAirNow(cityName)
            }
            if (result is BaseResult.Success) {
                airNow.postValue(result.data)
            } else if (result is BaseResult.Error) {
                mExceptionLiveData.postValue(result.exception)
            }
        }

        launchUI {
            val result = withContext(Dispatchers.IO) {
                airRepository.getAirDay(cityName)
            }
            if (result is BaseResult.Success) {
                airDay.postValue(result.data)
            } else if (result is BaseResult.Error) {
                mExceptionLiveData.postValue(result.exception)
            }
        }


    }
}